/**
 * JAVACC DEMO 1.0
 */
package com.apache.client;

import java.util.HashMap;
import java.util.Map;

import com.apache.api.vo.ResultEntity;
import com.apache.tools.ConfigUtil;
import com.apache.uct.common.LoginUser;
import com.apache.uct.common.ToolsUtil;

/**
 * description:  新的用户中心客户端操作类
 *
 * @author ius 创建时间：2016-3-30
 */
public class UctCoreClient {

	private static final String UCT_BEAN_ID = "uctService";

	private static String sysPass = ConfigUtil.getInstance().interfacePass();

	private static Map<String, String> conf_Map = new HashMap<String, String>();

	public static Map<String, String> getConf_Map() {

		/*if (conf_Map.isEmpty()) {
			conf_Map = getConfig();
		}*/
		conf_Map = ConfigUtil.getInstance().getUnityMap();
		return conf_Map;
	}

	/**
	 * 获取用户中心统一配置文件
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, String> getConfig() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sysPass", sysPass);
		ResultEntity root = UctProtoclClient.getInstance().doService(UCT_BEAN_ID, "unitySystemConfig", map);
		if (!ToolsUtil.isEmpty(root.getEntity())) {
			return (Map<String, String>) root.getEntity();
		}
		return new HashMap<String, String>();
	}

	/**
	 * 通过反射获取资源信息
	 *
	 * @return
	 */
	public static ResultEntity searchLockByReflect(Map<String, Object> map) {
		map.put("sysPass", sysPass);
		ResultEntity root = UctProtoclClient.getInstance().doService(UCT_BEAN_ID, "lockList", map);
		return root;
	}

	/**
	 * 通过反射获取用户信息
	 *
	 * @return
	 */
	public static ResultEntity searchUserByReflect(Map<String, Object> map) {
		map.put("sysPass", sysPass);
		ResultEntity root = UctProtoclClient.getInstance().doService(UCT_BEAN_ID, "userList", map);
		return root;
	}

	/**
	 * 通过角色ID或角色标识获取用户列表
	 *
	 * @return
	 */
	public static ResultEntity findUsersByRoleIdOrRoleEname(String roleId, String roleEname) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (ToolsUtil.isNotNull(roleId))
			map.put("roleId", roleId);
		if (ToolsUtil.isNotNull(roleEname))
			map.put("roleEname", roleEname);
		return searchUserByReflect(map);
	}

	/**
	 * 通过反射获取机构信息(参数中需要有当前用户的ID)
	 *
	 * @return
	 */
	public static ResultEntity searchOrgByReflect(Map<String, Object> map) {
		map.put("sysPass", sysPass);
		ResultEntity root = UctProtoclClient.getInstance().doService(UCT_BEAN_ID, "orgList", map);
		return root;
	}

	/**
	 * 根据orgIds获取机构列表
	 *
	 * @param map
	 */
	public static ResultEntity getOrgListByOrgIds(Map<String, Object> map) {
		map.put("sysPass", sysPass);
		ResultEntity root = null;
		if (ToolsUtil.isEmpty(map.get("orgIds"))) {
			root = new ResultEntity();
			root.setResult("false");
			root.setMessage("map key参数orgIds未传递");
		} else {
			root = UctProtoclClient.getInstance().doService(UCT_BEAN_ID, "orgList", map);
		}
		return root;
	}

	/**
	 * 通过反射获取部门信息
	 *
	 * @return
	 */
	public static ResultEntity searchDeptByReflect(Map<String, Object> map) {
		map.put("sysPass", sysPass);
		ResultEntity root = UctProtoclClient.getInstance().doService(UCT_BEAN_ID, "deptList", map);
		return root;
	}

	/**
	 * 通过反射获取角色信息
	 *
	 * @return
	 */
	public static ResultEntity searchRoleByReflect(Map<String, Object> map) {
		map.put("sysPass", sysPass);
		ResultEntity root = UctProtoclClient.getInstance().doService(UCT_BEAN_ID, "roleList", map);
		return root;
	}

	/**
	 * 通过反射获取权限信息
	 *
	 * @return
	 */
	public static ResultEntity searchActByReflect(Map<String, Object> map) {
		map.put("sysPass", sysPass);
		ResultEntity root = UctProtoclClient.getInstance().doService(UCT_BEAN_ID, "actList", map);
		return root;
	}

	/**
	 * 按用户ID或角色ID获取权限列表
	 *
	 * @return
	 */
	public static ResultEntity findActsByUserIdOrRoleId(String userId, String roleId) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (ToolsUtil.isNotNull(userId))
			map.put("userId", userId);
		if (ToolsUtil.isNotNull(roleId))
			map.put("roleId", roleId);
		map.put("sysPass", sysPass);
		ResultEntity root = UctProtoclClient.getInstance().doService(UCT_BEAN_ID, "actList", map);
		return root;
	}

	/**
	 * 根据属性获取用户信息
	 *
	 * @return
	 */
	public static ResultEntity getUserByProperty(String propertyName, String propertyValue) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(propertyName, propertyValue);
		map.put("sysPass", sysPass);
		ResultEntity root = UctProtoclClient.getInstance().doService(UCT_BEAN_ID, "userEntity", map);
		return root;
	}

	/**
	 * 根据属性获取机构信息
	 *
	 * @return
	 */
	public static ResultEntity getOrgByProperty(String propertyName, String propertyValue) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("propertyName", propertyName);
		map.put("propertyValue", propertyValue);
		map.put("sysPass", sysPass);
		ResultEntity root = UctProtoclClient.getInstance().doService(UCT_BEAN_ID, "orgEntity", map);
		return root;
	}

	/**
	 * 根据属性获取部门信息(按部门标识查询时必须传属门所在机构ID或机构标识).
	 *
	 * @return
	 */
	public static ResultEntity getDeptByProperty(String propertyName, String propertyValue) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("propertyName", propertyName);
		map.put("propertyValue", propertyValue);
		map.put("sysPass", sysPass);
		ResultEntity root = UctProtoclClient.getInstance().doService(UCT_BEAN_ID, "deptEntity", map);
		return root;
	}

	/**
	 * 根据属性获取权限信息.
	 *
	 * @return
	 */
	public static ResultEntity getActByProperty(String propertyName, String propertyValue) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("propertyName", propertyName);
		map.put("propertyValue", propertyValue);
		map.put("sysPass", sysPass);
		ResultEntity root = UctProtoclClient.getInstance().doService(UCT_BEAN_ID, "actEntity", map);
		return root;
	}

	/**
	 * description:  添加新机构
	 *
	 * @param map 方法参数(key为Org对象属性名)
	 */
	public static ResultEntity saveOrgInfo(Map<String, Object> map) {
		map.put("sysPass", sysPass);
		//map.put("exeType", "save");
		ResultEntity root = UctProtoclClient.getInstance().doService(UCT_BEAN_ID, "orgSave", map);
		return root;
	}

	/**
	 * description:  添加新用户
	 *
	 * @param map 方法参数(key为User对象属性名)
	 */
	public static ResultEntity saveUserInfo(Map<String, Object> map) {
		map.put("sysPass", sysPass);
		map.put("exeType", "save");
		map.put("checkCloumn", ToolsUtil.getInstance().getValueByKey("check_cloumn"));
		ResultEntity root = UctProtoclClient.getInstance().doService(UCT_BEAN_ID, "saveUser", map);
		return root;
	}

	/**
	 * description:  修改用户信息
	 *
	 * @param map 方法参数(key为User对象属性名)
	 */
	public static ResultEntity editUserInfo(Map<String, Object> map) {
		map.put("sysPass", sysPass);
		map.put("exeType", "edit");
		map.put("checkCloumn", ToolsUtil.getInstance().getValueByKey("check_cloumn"));
		ResultEntity root = UctProtoclClient.getInstance().doService(UCT_BEAN_ID, "saveUser", map);
		return root;
	}

	/**
	 * 授予用户权限
	 *
	 * @return
	 */
	public static ResultEntity grantActToUser(Map<String, Object> map) {
		map.put("sysPass", sysPass);
		map.put("doAdd", "true");
		ResultEntity root = UctProtoclClient.getInstance().doService(UCT_BEAN_ID, "grantActToUser", map);
		return root;
	}

	/**
	 * 剥夺用户权限
	 *
	 * @param map
	 * @return
	 */
	public static ResultEntity revokeActFromUser(Map<String, Object> map) {
		map.put("sysPass", sysPass);
		map.put("doAdd", "false");
		ResultEntity root = UctProtoclClient.getInstance().doService(UCT_BEAN_ID, "grantActToUser", map);
		return root;
	}

	/**
	 * description: 根据用户名及passport分配的token值,从uc获取用户信息
	 */
	public static LoginUser getLoginUserFromUserCenterSso(String userEname, String token) {
		LoginUser loginUser = null;
		//重新从用户中心获取loginUser
		Map<String, Object> map = new HashMap<String, Object>();
		if (ToolsUtil.isNotNull(userEname)) {
			map.put("userEname", userEname);
		}
		map.put("token", token);
		map.put("sysPass", sysPass);
		ResultEntity root = UctProtoclClient.getInstance().doService(UCT_BEAN_ID, "loginUserInfoSso", map);
		if ("true".equals(root.getResult())) {
			loginUser = (LoginUser) root.getEntity();
		}
		return loginUser;
	}

	/**
	 * 根据属性获取机构信息
	 *
	 * @return
	 */
	public static ResultEntity getSysByProperty(String propertyName, String propertyValue) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("propertyName", propertyName);
		map.put("propertyValue", propertyValue);
		map.put("sysPass", sysPass);
		ResultEntity root = UctProtoclClient.getInstance().doService(UCT_BEAN_ID, "sysEntity", map);
		return root;
	}

	/**
	 * 通过反射获取权限信息
	 *
	 * @return
	 */
	public static ResultEntity searchSysByReflect(Map<String, Object> map) {
		map.put("sysPass", sysPass);
		ResultEntity root = UctProtoclClient.getInstance().doService(UCT_BEAN_ID, "sysList", map);
		return root;
	}
}
